package com.sam.im.heiqun.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.UserEntivity;
import com.sam.im.heiqun.uis.activities.NewPasswordActivity;
import com.yuyh.library.utils.data.safe.MD5;
import com.yuyh.library.utils.toast.ToastUtils;
import com.yuyh.library.view.text.PasswordEditText;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by zhangqiang on 2018/7/3.
 */
public class DialogUtils {


    private static void showSetPSWdialog(final Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(context.getResources().getString(R.string.tip_psw));
        builder.setMessage(context.getResources().getString(R.string.no_pay_psw));
        builder.setNegativeButton(context.getResources().getString(R.string.now_no), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.setPositiveButton(context.getResources().getString(R.string.now_go), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(context, NewPasswordActivity.class);
                intent.putExtra("type", context.getResources().getString(R.string.set_pay_psd));
                context.startActivity(intent);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    public static void showPayDialog(final Context context, final ResultCallback<String> callback) {

        final UserEntivity user = ToolsUtils.getUser();
        if(user == null){
            return;
        }
        if (user.getPayInfo() == null || user.getPayInfo().equals("")) {
            showSetPSWdialog(context);
            return;
        }

        View dialog = LayoutInflater.from(context).inflate(R.layout.transfer_account_dialog, null);
        final AlertDialog payDialog;
        try {
            payDialog = new AlertDialog.Builder(context).setView(dialog).create();
            payDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        TextView payCancel = (TextView) dialog.findViewById(R.id.pay_cancel);
        TextView payCommit = (TextView) dialog.findViewById(R.id.pay_commit);
//        payPSD = ((EditText) dialog.findViewById(R.id.pay_psd));
        final PasswordEditText payPSD = ((PasswordEditText) dialog.findViewById(R.id.passwordInputView));
        payPSD.setSpacingWidth(0);
        popupKeyboard(payPSD);
        payCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                payDialog.dismiss();
            }
        });
        payCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String pwd = payPSD.getText().toString().trim();
                if ("".equals(pwd)) {
                    new ToastUtils().showLongToast(context.getResources().getString(R.string.please_import_pay_psd));
                } else {
                    if(callback != null){
                        callback.onGetResult(MD5.MD532(pwd));
                    }
                    payDialog.dismiss();
                }
            }
        });
    }

    private static void popupKeyboard(final PasswordEditText editText) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               if (inputManager == null) {
                                   return;
                               }
                               inputManager.showSoftInput(editText, 0);
                           }

                       },
                200);
    }

    public interface ResultCallback<T>{

        void onGetResult(T result);
    }
}
